$(document).ready(function() { 
	
	$('#allGroups').change(
		function() {
			$.getJSON('/dziennik/grades/subjects', {
				id : $(this).val(),
				ajax : 'true'
			}, function(data) {
				var html = '';
				var len = data.length;
				for ( var i = 0; i < len; i++) {
					console.log(data[i]);
					html += '<option value="' + data[i].id + '">'
							+ data[i].subjectName + '</option>';
				}
				html += '</option>';
				var selectData = [];

				for (var i=0; i < len; i++) {
				    var newElement = {};
				    newElement['id'] = data[i].id;
				    newElement['text'] = data[i].subjectName;
				    selectData.push(newElement);
				}

				$('#subjectsInGroup').html(html);
				$('#s2id_subjectsInGroup').find('.select2-chosen').text(data[0].subjectName);

			});
			
			$.getJSON('/dziennik/grades/students', {
				id : $(this).val(),
				ajax : 'true'
			}, function(data) {
				var html = '';
				var len = data.length;
				for ( var i = 0; i < len; i++) {
					console.log(data[i]);
					html += '<option value="' + data[i].id + '">'
					+ data[i].studentFirstName + ' ' + data[i].studentLastName  + '</option>';
				}
				html += '</option>';
				
				$('#studentsInGroup').html(html);
			});
		});
	
		$('#subjectsInGroup').change(
		function() {
			console.log("selected: " + $('#subjectsInGroup option:selected').text() +" " + this.value);
			
		});
		
		$('#studentsInGroup').change(
				function() {
					console.log("selected: " + $('#studentsInGroup option:selected').text() +" " + this.value);
					
				});
});